﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:ADU_CONSUM
	/// </summary>
	public partial class ADU_CONSUM : BaseDAL<GSLMS.Entity.TB_ADU_CONSUM>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public ADU_CONSUM()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_ADU_CONSUM model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_PROID", model.PROID),
								new OracleParameter("P_ARRID", model.ARRID),
								new OracleParameter("P_SUMOPINION", model.SUMOPINION),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("P_SUMBY", model.SUMBY),
								new OracleParameter("P_SUMDATE", model.SUMDATE),
								new OracleParameter("P_ADURESULT", model.ADURESULT),
								new OracleParameter("P_TOUSER", model.TOUSER)};
  
			for (int i = 0; i < 9; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_audit.proc_consum_add", parms);
                if (model.ADURESULT==1&&model.AduConSchemeList != null && model.AduConSchemeList.Count > 0)
                {
                    flag = AddConsCheMeList(model.AduConSchemeList,model.PROID);
                }
                else
				  flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

        /// <summary>
        ///  增加审议汇总的方案信息
        /// </summary>
        public bool AddConsCheMeList(List<GSLMS.Entity.TB_ADU_CONSCHEME> list,long proID)
        {
            bool flag = false;
            int n = 1;
            if (list != null && list.Count > 0)
            {
                foreach (var model in list)
                {
                    OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_PROID", proID),
								new OracleParameter("P_ARRID", model.ARRID),
								new OracleParameter("P_PROGNO", n++),
								new OracleParameter("P_PROGCAT", model.PROGCAT),
								new OracleParameter("P_AMT", model.AMT),
								new OracleParameter("P_RATE", model.RATE),
								new OracleParameter("P_MANAGERATE", model.MANAGERATE),
								new OracleParameter("P_GENERALRATE", model.GENERALRATE),
								new OracleParameter("P_LOANTIME", model.LOANTIME),
								new OracleParameter("P_LOANPHASE", model.LOANPHASE),
								new OracleParameter("P_LOANPHASETYPE", model.LOANPHASETYPE),
								new OracleParameter("P_ADVAMT", model.ADVAMT),
								new OracleParameter("P_AUDITREMARK", model.AUDITREMARK)};

                    for (int i = 0; i < 14; i++)
                    {
                        parms[i].Direction = ParameterDirection.Input;
                    }

                    try
                    {
                        OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_audit.proc_conscheme_add", parms);
                        flag = true;
                    }
                    catch (Exception ex)
                    {
                        log.ExceptionHand(ex);
                        flag = false;
                        break;

                    }
                }
            }
            else
                flag = true;
            return flag;
        }

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_ADU_CONSUM model)
		{
			bool flag = false;
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_ID", model.ID),
            //                    new OracleParameter("P_PROID", model.PROID),
            //                    new OracleParameter("P_ARRID", model.ARRID),
            //                    new OracleParameter("P_SUMOPINION", model.SUMOPINION),
            //                    new OracleParameter("P_REMARK", model.REMARK),
            //                    new OracleParameter("P_SUMBY", model.SUMBY),
            //                    new OracleParameter("P_SUMDATE", model.SUMDATE),
            //                    new OracleParameter("P_ADURESULT", model.ADURESULT),
            //                    new OracleParameter("P_TOUSER", model.TOUSER)};
  
            //for (int i = 0; i < 9; i++)
            //{
            //    parms[i].Direction = ParameterDirection.Input;
            //}

            //try
            //{
            //    OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_consum_update", parms);
            //    flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

       

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_ID", ID)};
  
            //    parms[0].Direction = ParameterDirection.Input;

            //try
            //{
            //    OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_consum_delete", parms);
            //    flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_ADU_CONSUM DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_ADU_CONSUM model = new GSLMS.Entity.TB_ADU_CONSUM();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["PROID"]!=null && dr["PROID"].ToString()!="")
				{
					model.PROID=OracleHelper.ToLong(dr["PROID"].ToString());
				}
				if(dr["ARRID"]!=null && dr["ARRID"].ToString()!="")
				{
					model.ARRID=OracleHelper.ToLong(dr["ARRID"].ToString());
				}
				if(dr["SUMOPINION"]!=null && dr["SUMOPINION"].ToString()!="")
				{
					model.SUMOPINION=dr["SUMOPINION"].ToString();
				}
				if(dr["REMARK"]!=null && dr["REMARK"].ToString()!="")
				{
					model.REMARK=dr["REMARK"].ToString();
				}
				if(dr["SUMBY"]!=null && dr["SUMBY"].ToString()!="")
				{
					model.SUMBY=dr["SUMBY"].ToString();
				}
				if(dr["SUMDATE"]!=null && dr["SUMDATE"].ToString()!="")
				{
					model.SUMDATE=DateTime.Parse(dr["SUMDATE"].ToString());
				}
				if(dr["ADURESULT"]!=null && dr["ADURESULT"].ToString()!="")
				{
                    model.ADURESULT = OracleHelper.ToInt(dr["ADURESULT"].ToString());
				}
				if(dr["TOUSER"]!=null && dr["TOUSER"].ToString()!="")
				{
					model.TOUSER=dr["TOUSER"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_ADU_CONSUM GetByID(long ID)
		{
			GSLMS.Entity.TB_ADU_CONSUM sModel = new GSLMS.Entity.TB_ADU_CONSUM();
			string sql = "";
			sql = "select * from TB_ADU_CONSUM where id=" + ID.ToString();

			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

        /// <summary>
        /// 通过立项申请记录ID提取贷款审议汇总信息
        /// </summary>
        /// <param name="ProID"></param>
        /// <returns></returns>
        public GSLMS.Entity.TB_ADU_CONSUM GetByProID(long ProID)
        {
            GSLMS.Entity.TB_ADU_CONSUM sModel = new GSLMS.Entity.TB_ADU_CONSUM();
           

            OracleParameter[] parms = new OracleParameter[] { 
              new OracleParameter("p_proid",ProID),
              new OracleParameter("p_cur",OracleType.Cursor),
              new OracleParameter("p_cscur",OracleType.Cursor)
            };

            parms[0].Direction = ParameterDirection.Input;
            parms[1].Direction = ParameterDirection.Output;
            parms[2].Direction = ParameterDirection.Output;



            DataSet ds = null;

            ds=OracleHelper.ExecuteDataset(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_audit.proc_consum_get", parms);
            

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                sModel = DataRowToModel(ds.Tables[0].Rows[0]);
                if (ds.Tables.Count > 1&&ds.Tables[1].Rows.Count>0)
                {
                    var conschemeList = new List<GSLMS.Entity.TB_ADU_CONSCHEME>();

                    for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                    {
                        var conschemeModel = new GSLMS.Entity.TB_ADU_CONSCHEME();
                        conschemeModel = DataRowToCONSCHEMEModel(ds.Tables[1].Rows[i]);
                        conschemeList.Add(conschemeModel);
                    }

                    sModel.AduConSchemeList = conschemeList;
                }
            }
            else
            {
                return null;
            }
            return sModel;
        }

        /// <summary>
        ///DataRowToModel 
        /// </summary>
        private GSLMS.Entity.TB_ADU_CONSCHEME DataRowToCONSCHEMEModel(DataRow dr)
        {
            if (dr != null && dr.ItemArray.Length > 0)
            {
                GSLMS.Entity.TB_ADU_CONSCHEME model = new GSLMS.Entity.TB_ADU_CONSCHEME();
                if (dr["ID"] != null && dr["ID"].ToString() != "")
                {
                    model.ID = OracleHelper.ToLong(dr["ID"].ToString());
                }
                if (dr["PROID"] != null && dr["PROID"].ToString() != "")
                {
                    model.PROID = OracleHelper.ToLong(dr["PROID"].ToString());
                }
                if (dr["ARRID"] != null && dr["ARRID"].ToString() != "")
                {
                    model.ARRID = OracleHelper.ToLong(dr["ARRID"].ToString());
                }
                if (dr["PROGNO"] != null && dr["PROGNO"].ToString() != "")
                {
                    model.PROGNO = OracleHelper.ToInt(dr["PROGNO"].ToString());
                }
                if (dr["PROGCAT"] != null && dr["PROGCAT"].ToString() != "")
                {
                    model.PROGCAT = dr["PROGCAT"].ToString();
                }
                if (dr["AMT"] != null && dr["AMT"].ToString() != "")
                {
                    model.AMT = OracleHelper.ToDecimal(dr["AMT"].ToString());
                }
                if (dr["RATE"] != null && dr["RATE"].ToString() != "")
                {
                    model.RATE = OracleHelper.ToDecimal(dr["RATE"].ToString());
                }
                if (dr["MANAGERATE"] != null && dr["MANAGERATE"].ToString() != "")
                {
                    model.MANAGERATE = OracleHelper.ToDecimal(dr["MANAGERATE"].ToString());
                }
                if (dr["GENERALRATE"] != null && dr["GENERALRATE"].ToString() != "")
                {
                    model.GENERALRATE = OracleHelper.ToDecimal(dr["GENERALRATE"].ToString());
                }
                if (dr["LOANTIME"] != null && dr["LOANTIME"].ToString() != "")
                {
                    model.LOANTIME = OracleHelper.ToInt(dr["LOANTIME"].ToString());
                }
                if (dr["LOANPHASE"] != null && dr["LOANPHASE"].ToString() != "")
                {
                    model.LOANPHASE = OracleHelper.ToInt(dr["LOANPHASE"].ToString());
                }
                if (dr["LOANPHASETYPE"] != null && dr["LOANPHASETYPE"].ToString() != "")
                {
                    model.LOANPHASETYPE = OracleHelper.ToInt(dr["LOANPHASETYPE"].ToString());
                }
                if (dr["ADVAMT"] != null && dr["ADVAMT"].ToString() != "")
                {
                    model.ADVAMT = OracleHelper.ToDecimal(dr["ADVAMT"].ToString());
                }
                if (dr["AUDITREMARK"] != null && dr["AUDITREMARK"].ToString() != "")
                {
                    model.AUDITREMARK = dr["AUDITREMARK"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }


		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_ADU_CONSUM> GetList(GSLMS.Entity.TB_ADU_CONSUM searchParam)
		{
			var list=new List<GSLMS.Entity.TB_ADU_CONSUM>();
			DataTable dt = null;
			string tblName = "TB_ADU_CONSUM";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;
			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_ADU_CONSUM model = new GSLMS.Entity.TB_ADU_CONSUM();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

